package com.example.a79273.shandianban;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.adapter.YouHuo_TitlePagerAdapter;
import com.example.a79273.shandianban.config.NetconfigYouHuo;
import com.example.a79273.shandianban.fragment.EvaluateFragment;
import com.example.a79273.shandianban.fragment.ParamsFragment;
import com.example.a79273.shandianban.fragment.ParticularsFragment;

import java.util.ArrayList;
import java.util.List;

public class ProductInfoActivity extends BaseActivity implements View.OnClickListener {
    private String proid;
    private String mPath;
    private View mViewTablayout;
    private ImageView mImageViewBack,mImageViewShare;//Tablaout上的返回和分享
    private TextView mTextViewTitle;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String> mTitles;
    private List<Fragment> mFragments;
    private YouHuo_TitlePagerAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_info);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mImageViewBack.setOnClickListener(this);
        mImageViewShare.setOnClickListener(this);

    }

    private void setData() {

        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    private void initData() {
        //获取传来的信息并拼接地址
        proid = getIntent().getStringExtra("proid");
        mPath = NetconfigYouHuo.PRODUCT_INFO+proid+"/";
       // mPath = String.format(NetconfigYouHuo.PRODUCT_INFO,proid);
        //Toast.makeText(ProductInfoActivity.this, ""+proid, Toast.LENGTH_SHORT).show();
        mTitles = new ArrayList<>();
        mFragments = new ArrayList<>();
        mAdapter = new YouHuo_TitlePagerAdapter(getSupportFragmentManager(),mFragments,mTitles);
        mTitles.add("商品详情");
        mTitles.add("参数规格");
        mTitles.add("评价");
        //初始化fragment集合
        for (int i = 0; i < mTitles.size(); i++) {
          Fragment fragment = null;
            switch (i) {
                case  0:
                    fragment = new ParticularsFragment();
                    break;
                case 1:
                    fragment = new ParamsFragment();
                    break;
                case 2:
                    fragment = new EvaluateFragment();
                    break;
            }
            Bundle bundle = new Bundle();
            bundle.putString("path",mPath);
            bundle.putString("proid",proid);
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }
    }

    private void initView() {
        mViewTablayout = (View) findViewById(R.id.in_product_info_tablayout);
        mImageViewBack = (ImageView) mViewTablayout.findViewById(R.id.iv_tablayout_back);
        mImageViewBack.setImageResource(R.mipmap.return_icon);
        mImageViewShare = (ImageView) mViewTablayout.findViewById(R.id.iv_tablayout_search);
        mImageViewShare.setImageResource(R.mipmap.activity_icon03);
        mTextViewTitle = (TextView) mViewTablayout.findViewById(R.id.tv_tablayout_title);
        mTextViewTitle.setText("商品详情");
        mTabLayout = (TabLayout) findViewById(R.id.tab_product_info_title);
        mViewPager = (ViewPager) findViewById(R.id.vp_product_info_body);
    }

    //所有的点击事件
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
          case  R.id.iv_tablayout_back:
                 finish();
              break;
          case R.id.iv_tablayout_search://TODO 第三方分享

              break;
      }
    }
}
